package atom.core2.j2ee.tag;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import atom.core2.j2ee.tag.util.TagUtil;
import atom.core2.j2ee.util.SessionManager;

/**
 * @author aps-zmc
 * 
 */
public class ButtonPrivilegeTag extends TagSupport
{
	private static final long	serialVersionUID	= 1L;

	/**
	 * privilge id.
	 */
	private String	          pid;

	public void setPid(String pid)
	{
		this.pid = pid;
	}

	public int doEndTag() throws JspException
	{
		
		TagUtil.write(pageContext, "</span>");
		return EVAL_PAGE;
	}

	public int doStartTag() throws JspException
	{
		HttpSession session = (HttpSession) ((HttpServletRequest) this.pageContext.getRequest()).getSession();
		Map<String, String> privileges = (Map<String, String>) session.getAttribute(SessionManager.BUTTON_PRIVILEGE);
		String span;
		if (privileges != null)
		{
			String url = privileges.get(pid);
			if (url == null)
				span = "<span class='no-privilege' >";
			else
				span = "<span class='has-privilege' >";
		}
		else
		{
			span = "<span class='no-privilege' >";
		}
		TagUtil.write(pageContext, span);
		return EVAL_BODY_INCLUDE;
	}
}
